app.controller('editCourse', ['$scope','$modal','$http','$rootScope','$state','$timeout','$sessionStorage', function($scope,$modal,$http,$rootScope,$state,$timeout,$sessionStorage){
	$scope.courseId=$sessionStorage.courseId;
	var $coverImg=$("#coverImg");

	$http({
	    url:$rootScope.baseUrl+"Course/getCourseTheme",
	    method:"post",
	}).then(function(res){
	    if(res.data.code==200){
	        $scope.getCourseThemeList=res.data.data;

	        $http({
	            url:$rootScope.baseUrl+"Course/getCourseInfo",
	            method:"post",
	            data:{
	            	course_id:$scope.courseId,
	            }
	        }).then(function(res){
	            if(res.data.code==200){
	            	var data=res.data.data;
	        		$scope.courseThemeID=data.course_theme_id;

	        		$scope.name=data.name;
	        		$scope.introduce=data.introduce;
	        		$coverImg.attr("src",data.img_path);

	        		$.each($scope.getCourseThemeList,function(index,item){
	        			if($scope.courseThemeID==item.id){
	        				$scope.courseTheme=item.name;
	        			}
	        		});
	            }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
	        },function(error){
	            console.log(error);
	        });
	    }
	},function(error){
	    console.log(error);
	});

	$http({
	    url:$rootScope.baseUrl+"Course/getCourseHourList",
	    method:"post",
	    data:{
	    	course_id:$scope.courseId,
	    }
	}).then(function(res){
	    if(res.data.code==200){
			$scope.videoList=res.data.data;
			if($scope.videoList.length==0){
				$rootScope.noData=true;
			}else {
				$.each($scope.videoList,function(index,item){
					item.sort=parseFloat(item.sort);
				});
				$rootScope.noData=false;
			}
	    }else if(res.data.code==409){
	    	$state.go("access.signin");
	    }
	},function(error){
	    console.log(error);
	});

	$scope.selectCourseTheme=function(item){
		$scope.courseTheme=item.name;
		$scope.courseThemeID=item.id;
	}
	
	// 添加课时
	$scope.courseHour=[];
	$scope.addCourseUpload=function(){
		var data={
			name:"",
			link:"",
			sort:""
		}
		$scope.courseHour.push(data);
		$rootScope.noData=false;
	}

	$("#cover").on("change",function(event){
		var files=event.target.files;

		$.each(files,function(index,file){
			var reader = new FileReader();  
	        reader.onload = function (event) { 
	            $coverImg.attr("src",event.target.result);
	        }
	        reader.readAsDataURL(file); 
		});
	});

	// 取消添加课程
	$scope.cancelAdd=function(){
		swal({
		    title: "确定取消编辑该课程？",
		    type: "warning",
		    showCancelButton: true,
		    confirmButtonColor: "#619ce1",
		    confirmButtonText: "确定",
		    closeOnConfirm: true
		},
		function() {
		    $state.go("fullScore.course");
		});
	}

	$scope.openCourseInfo=function(){
		$scope.courseInfo=true;
	}

	// 确定保存
	$scope.courseInfo=true;
	$scope.sureAdd=function(){
		if($scope.courseThemeID==undefined){
			swal("请选择课程分类");
		}else {
			var formData = new FormData($("#uploadCover")[0]);

			$.ajax({
			    url: $rootScope.baseUrl +"Course/editCourse",
			    type: 'post',
			    data: formData,
			    xhrFields: {
	                withCredentials: true
	            },
			    async: false,
			    cache: false,
			    contentType: false,
			    processData: false,
			    global: false,
			    success: function(res) {
			        if(res.code==200){
			        	$scope.courseId=res.data.course_id;
			        	$scope.courseInfo=false;
			        }
			    },
			    error: function(error) {

			    }
			});
		}
	}
}]);

// 新添加课时
app.controller('addCourseUpload', ['$scope','$modal','$http','$rootScope','$state','$timeout', function($scope,$modal,$http,$rootScope,$state,$timeout){
	$scope.hideBtn=true;
	// 确定添加课时
	$scope.sureAddCourse=function(index,event){
		var $table=$(".table-striped").eq(index);
		var $hash=$table.find(".info").find(".hash");
		var href=$hash.prev().find("a").attr("href");
		var $progress=$table.find(".status.text-left");

		if(href){
			$scope.href=href;
		}

		if(!$scope.courseHourName){
			swal("请填写课时名称");
			return;
		}else if($progress.css("display")=="block"){
			swal("视频还在上传中");
			return;
		}else if(!$scope.courseHourSort){
			swal("请填写课时排序");
			return;
		}else {
			var data={
		    	course_id:$scope.courseId,
		    	course_hour_name:$scope.courseHourName,
		    	url:$scope.href,
		    	sort:$scope.courseHourSort
		    }
		    if($scope.courseHourId){
		    	data.course_hour_id=$scope.courseHourId;
		    	var url="Course/editCourseHour";
		    }else {
		    	var url="Course/addCourseHour";
		    }
			$http({
			    url:$rootScope.baseUrl+url,
			    method:"post",
			    data:data
			}).then(function(res){
			    if(res.data.code==200){
			    	if(!$scope.courseHourId){
			    		$scope.courseHourId=res.data.data.course_hour_id;
			    	}

			    	var $td=$(event.target).closest("td");
			    	var $infoCourse=$td.find("p.infoCourse");
			    	var $bigDiv=$td.find(".bigDiv");
			    	$.each($infoCourse,function(index,ele){
			    		var $input=$(ele).find("input");

			    		var $span=$(ele).find("span");
			    		$input.hide();
			    		$bigDiv.hide();
			    		$span.show();
			    		$scope.hideBtn=false;
			    	});

			    	$scope.courseHourList=[];
			    	var info={
			    		name:$scope.courseHourName,
			    		link:$scope.href,
			    		sort:$scope.courseHourSort
			    	}
			    	$scope.courseHour[index]=info;
			    }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
			},function(error){
			    console.log(error);
			});
		}
	}


	// 编辑课时
	$scope.courseHourList=[];
	$scope.editCourseHour=function(index,event){
		if(!$scope.courseHourId){
			sweetAlert("编辑失败", "课时还没保存", "error");
		}else if(!$scope.hideBtn){
			var $td=$(event.target).closest("td").prev();
			var $infoCourse=$td.find("p.infoCourse");
			var $bigDiv=$td.find(".bigDiv");

			$.each($infoCourse,function(index,ele){
				var $input=$(ele).find("input");
				
				var $span=$(ele).find("span");

				$input.show();
				$bigDiv.remove();
				$span.hide();
				$scope.hideBtn=true;
			});
			$scope.courseHourList.length=1;
		}
	}

	// 删除课时
	$scope.deleteCourseHour=function(index,event){
		if($scope.courseHourId){
			swal({
			    title: "确定删除该课时？",
			    type: "warning",
			    showCancelButton: true,
			    confirmButtonColor: "#619ce1",
			    confirmButtonText: "确定",
			    closeOnConfirm:true
			},
			function() {
			    $http({
			        url:$rootScope.baseUrl+"Course/delCourseHour",
			        method:"post",
			        data:{
			        	course_id:$scope.courseId,
			        	course_hour_id:$scope.courseHourId,
			        }
			    }).then(function(res){
			        if(res.data.code==200){
			        	swal("刪除成功", "", "success");
			        	$timeout(function(){
			        		$scope.courseHour.splice(index,1);
			        		if($scope.courseHour.length==0 && $scope.videoList.length==0){
			        			$rootScope.noData=true;
			        		}
			        	},30);

			        	var $td=$(event.target).closest("td").prev();
			        	var $infoCourse=$td.find("p.infoCourse");
			        	var $bigDiv=$td.find(".bigDiv");

			        	$.each($infoCourse,function(index,ele){
			        		var $input=$(ele).find("input");
			        		
			        		var $span=$(ele).find("span");

			        		$input.show();
			        		$bigDiv.remove();
			        		$span.hide();
			        		$scope.hideBtn=true;
			        	});
			        	$scope.courseHourList.length=1;
			        	$scope.courseHourName="";
			        	$scope.href="";
			        	$scope.courseHourSort="";
			        	$scope.courseHourId="";
			        }else if(res.data.code==409){
				    	$state.go("access.signin");
				    }
			    },function(error){
			        console.log(error);
			    });	
			});
		}else {
			sweetAlert("课时还没保存", "不需要删除", "error");
		}
	}

	// 获取随机数
	$scope.getRandom = function(num) {
	    var str = "";
	    var strNum = "abcdefghilklmnopqrstuvwxyz";
	    for (var i = 0; i < num; i++) {
	        str += strNum.substr(parseInt(Math.random() * strNum.length), 1);
	    }
	    return str;
	}
	$scope.randomStr=$scope.getRandom(5);
}]);

// 修改已有课时
app.controller('videoListUpload', ['$scope','$modal','$http','$rootScope','$state','$timeout', function($scope,$modal,$http,$rootScope,$state,$timeout){
	$scope.hideBtn=false;
	// 确定添加课时
	$scope.sureAddCourse=function(item,index,event){
		var $table=$(".table-striped").eq(index);
		var $hash=$table.find(".info").find(".hash");
		var href=$hash.prev().find("a").attr("href");
		var $progress=$table.find(".status.text-left");

		if(href){
			$scope.href=href;
		}else {
			$scope.href=item.video_url;
		}

		if(!item.name){
			swal("请填写课时名称");
			return;
		}else if(!item.sort){
			swal("请填写课时排序");
			return;
		}else if($progress.css("display")=="block"){
			swal("视频还在上传中");
			return;
		}else{
			var data={
		    	course_id:$scope.courseId,
		    	course_hour_id:item.id,
		    	course_hour_name:item.name,
		    	url:$scope.href,
		    	sort:item.sort
		    }

			$http({
			    url:$rootScope.baseUrl+"Course/editCourseHour",
			    method:"post",
			    data:data
			}).then(function(res){
			    if(res.data.code==200){
			    	var $td=$(event.target).closest("td");
			    	var $infoCourse=$td.find("p.infoCourse");
			    	var $bigDiv=$td.find(".bigDiv");
			    	var arr=[$scope.courseHourName,$scope.href,$scope.courseHourSort];
			    	$.each($infoCourse,function(index,ele){
			    		var $input=$(ele).find("input");
			    		
			    		var $span=$(ele).find("span").html(arr[index]);

			    		$input.hide();
			    		$bigDiv.hide();
			    		$span.show();
			    		$scope.hideBtn=false;
			    	});
			    	$scope.courseHourList=[];
			    }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
			},function(error){
			    console.log(error);
			});
		}
	}

	// 编辑课时
	$scope.courseHourList=[];
	$scope.editCourseHour=function(item,index,event){
		$scope.courseHourId=item.id;

		if(!$scope.hideBtn){
			var $td=$(event.target).closest("td").prev();
			var $infoCourse=$td.find("p.infoCourse");

			$.each($infoCourse,function(index,ele){
				var $input=$(ele).find("input");
				
				var $span=$(ele).find("span");

				$input.show();
				$span.hide();
				$scope.hideBtn=true;
			});
			$scope.courseHourList.length=1;
		}
	}

	// 删除课时
	$scope.deleteCourseHour=function(item,index){
		swal({
		    title: "确定删除该课时？",
		    type: "warning",
		    showCancelButton: true,
		    confirmButtonColor: "#619ce1",
		    confirmButtonText: "确定",
		    closeOnConfirm:true
		},
		function() {
		    $http({
		        url:$rootScope.baseUrl+"Course/delCourseHour",
		        method:"post",
		        data:{
		        	course_id:$scope.courseId,
		        	course_hour_id:item.id,
		        }
		    }).then(function(res){
		        if(res.data.code==200){
		        	swal("刪除成功", "", "success");
		        	$timeout(function(){
		        		$scope.videoList.splice(index,1);
		        		if($scope.videoList.length==0 && $scope.courseHour.length==0){
		        			$rootScope.noData=true;
		        		}
		        	},30);
		        }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
		    },function(error){
		        console.log(error);
		    });	
		});
	}

	// 获取随机数
	$scope.getRandom = function(num) {
	    var str = "";
	    var strNum = "abcdefghilklmnopqrstuvwxyz";
	    for (var i = 0; i < num; i++) {
	        str += strNum.substr(parseInt(Math.random() * strNum.length), 1);
	    }
	    return str;
	}
	$scope.randomStr=$scope.getRandom(5);
}]);